<template>
  <edit-layout ref="layout" :page="page" :form="form" @invoke="invoke">
    <el-form
      ref="form"
      :model="form"
      :size="page.settings.formSize"
      :label-width="page.settings.labelWidth"
    >
      <el-row>
        <el-col :span="8">
          <el-form-item label="名称" prop="name" :rules="page.rules.def">
            <el-input v-model="form.name" placeholder="请输入名称" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="编码" prop="no" :rules="page.rules.def">
            <el-input v-model="form.no" placeholder="请输入编码" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="8">
          <el-form-item label="地区" prop="areaIds" :rules="page.rules.def">
            <form-area v-model="form.areaIds" :area="form.area" :level="3" @set-area="setArea" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="详情地址" prop="address">
            <el-input v-model="form.address" type="textarea" placeholder="请输入详情地址" />
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="地址类型" prop="locationType">
        <el-radio-group v-model="form.locationType" :disabled="currentRole!='super'?true:false">
          <el-radio :label="0">纸厂地址</el-radio>
          <el-radio :label="1">运输地址</el-radio>
        </el-radio-group>
      </el-form-item>
      <div v-if="form.locationType==0">
        <!-- <el-row>
          <el-col :span="8">
            <el-form-item label="手机号码" prop="mobile" :rules="page.rules.def">
              <el-input v-model="form.mobile" placeholder="手机号码" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人" prop="linkman" :rules="page.rules.def">
              <el-input v-model="form.linkman" placeholder="联系人" />
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row>
          <el-col :span="8">
            <el-form-item label="接货员" prop="empId" :rules="page.rules.def">
              <duty-users
                v-if="form.id !=null || !form.id"
                v-model="form.empId"
                lable=""
                :duty-id="$store.state.settings.wmsConf.purchase.dutyId[2]"
                placeholder="请选择收获员"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="业务主管" prop="leaderId" :rules="page.rules.def">
              <duty-users
                v-if="form.id !=null || !form.id"
                v-model="form.leaderId"
                lable=""
                :duty-id="$store.state.settings.wmsConf.purchase.dutyId[0]"
                placeholder="请选择业务主管"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="出纳" prop="cashierId" :rules="page.rules.def">
              <duty-users
                v-if="form.id !=null || !form.id"
                v-model="form.cashierId"
                lable=""
                :duty-id="$store.state.settings.wmsConf.purchase.dutyId[4]"
                placeholder="请选择出纳"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="财务" prop="purchaseId" :rules="page.rules.def">
              <duty-users
                v-if="form.id !=null || !form.id"
                v-model="form.purchaseId"
                lable=""
                :duty-id="$store.state.settings.wmsConf.purchase.dutyId[3]"
                placeholder="请选择财务"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </div>
      <!-- 修改创建时间 -->
      <div v-if="form.id !=null">
        <el-row>
          <el-col :span="8">
            <el-form-item label="创建时间" prop="linkman">
              <label>{{ form.createTime }}</label>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="修改时间" prop="linkman">
              <label>{{ form.updateTime }}</label>
            </el-form-item>
          </el-col>
        </el-row>
      </div>

    </el-form>
  </edit-layout>
</template>
<script>
import adminSupport from 'lola-admin-support-el'

export default {
  name: 'LocationEdit',
  mixins: [adminSupport.mixins.edit],
  data() {
    return {
      name: 'Location'
    }
  },
  computed: {
    // 当前登录用户的部门id
    currentUserDeptId: function() {
      return parseInt(this.$store.getters.token.deptId)
    },
    // 判断当前用户权限
    currentRole: function() {
      var conf = this.$store.state.settings.wmsConf
      let tmp = 'super'
      Object.keys(conf).forEach(val => {
        if (conf[val] && parseInt(conf[val].deptId) === this.currentUserDeptId) tmp = val
      })
      return tmp
    }
  },
  watch: {
    // 权限判断
    'form': function() {
      if (this.currentRole === 'logistics') { // 物流
        this.form.locationType = 1
      } else if (this.currentRole === 'purchase') { // 采购
        this.form.locationType = 0
      }
    }
  },
  mounted() {
    this.page.tip = '地址类型为纸厂必选业务页'
    this.init() // 初始化
  },
  methods: {
    init() {
      const that = this
      that.page.ids.push(this.$route.params.no) // 页面主参数赋值
      that.loadData() // 加载数据
    },
    appendForm() {
      const roleIds = []
      if (this.form.roles != null) {
        this.form.roles.forEach(item => {
          roleIds.push(item.id)
        })
      }
      this.form.roleIds = roleIds
    },
    buildForm() {
      return true
    },
    // 地区选择事件
    setArea(val) {
      console.log(val)
      this.form.area = val
    }
  }
}
</script>
